package com.css.dzj.zfjg.modules.statisticsa.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.zfjg.common.audit.service.SysAuditService;
import com.css.dzj.zfjg.common.util.Constant;
import com.css.dzj.zfjg.common.util.PlatformFileUtil;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.statisticsa.service.StatisticsaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/statisticsa")
public class StatisticsaController {
    @Autowired
    private StatisticsaService statisticsaService;

    @Autowired
    private PlatformFileUtil platformFileUtil;

    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 从Excel表格中批量导入数据到数据库
     *
     * @param file
     * @return
     */
    @PostMapping("/import")
    public R addFormExcel(@RequestParam("file") MultipartFile file) {
        String s = "";
        try {
            statisticsaService.addFromExcel(file);
            s = JSON.toJSONString("");
            sysAuditService.saveSuccessAudit("信用评估结果统计-A导入成功", Constant.ZFJG_STATISTICSA, Constant.PT, Constant.ZFJG_DEPTEXCEL, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("信用评估结果统计-A导入失败", Constant.ZFJG_STATISTICSA, Constant.YZ, Constant.ZFJG_DEPTEXCEL, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 模板下载
     */
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(){
        String s = "";
        try {
            String fileName = "信用评估结果统计-A模板.xlsx";
            platformFileUtil.download(fileName);
            s = JSON.toJSONString(fileName);
            sysAuditService.saveSuccessAudit("信用评估结果统计-A模板下载成功", Constant.ZFJG_PENALTY, Constant.PT, Constant.ZFJG_DOWNLOADTEMPLATE, s, request);
        }catch (Exception e){
            sysAuditService.saveFailAudit("信用评估结果统计-A模板下载失败", Constant.ZFJG_PENALTY, Constant.YZ, Constant.ZFJG_DOWNLOADTEMPLATE, s, e, request);
        }
    }
}
